package com.ningmengs789.live.ui.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ningmengs789.live.R;
import com.ningmengs789.live.http.JsonBuilder;
import com.ningmengs789.live.http.OKHttpUtils;
import com.ningmengs789.live.Interface.RequestCallback;
import com.ningmengs789.live.live.live.common.utils.DialogUitl;
import com.ningmengs789.live.oss.CosXmlUtils;
import com.ningmengs789.live.net.response.OssImageResponse;
import com.ningmengs789.live.net.response.OssVideoResponse;
import com.ningmengs789.live.net.utils.NToast;
import com.ningmengs789.live.ui.widget.RoundFrameLayout;
import com.ningmengs789.live.ui.widget.dialogorPopwindow.UploadDialog;
import com.ningmengs789.live.utils.CommonUtils;
import com.ningmengs789.live.utils.UserInfoUtil;
import com.bumptech.glide.Glide;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.common.ImageLoader;
import com.yuyh.library.imgsel.config.ISListConfig;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 * 日期:2018/10/24
 * 描述:实名认证
 **/
public class ShiMingRenZhengActivity extends BaseActivity implements View.OnClickListener, CosXmlUtils.OSSResultListener {

    public static final int REQUEST_CODE_SELECT = 1005;

    @BindView(R.id.tv_title_center)
    TextView tv_title_center;

    @BindView(R.id.renzhengPrewLayout)
    LinearLayout renzhengPrewLayout;
//    @BindView(R.id.renzhengSuccessLayout)
//    LinearLayout renzhengSuccessLayout;
//    @BindView(R.id.renzhengFaildLayout)
//    LinearLayout renzhengFaildLayout;

    @BindView(R.id.card1Layout)
    RoundFrameLayout card1Layout;
    @BindView(R.id.card2Layout)
    RoundFrameLayout card2Layout;

    @BindView(R.id.mengban1)
    ImageView mengban1;
    @BindView(R.id.mengban2)
    ImageView mengban2;

    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.realName)
    EditText realName;
    @BindView(R.id.cardNumber)
    EditText cardNumber;

    private boolean isCard=false;

//    @BindView(R.id.renZheng)
//    Button renZheng;

    private Uri selectUri;

    private String path1 = "", path2 = "";
    private String localPath1 = "", localPath2 = "";
    private int flag = -1;

    private CosXmlUtils uploadOssUtils;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shiming_renzheng);
        ButterKnife.bind(this);
        setHeadLayout();
        initView();
        initEvent();
    }

    private void initView() {
        tv_title_center.setVisibility(View.VISIBLE);
        tv_title_center.setText(R.string.shiming_title);
    }

    private void initEvent() {
        submit.setOnClickListener(this);
        card1Layout.setOnClickListener(this);
        card2Layout.setOnClickListener(this);
//        renZheng.setOnClickListener(this);

        userId = UserInfoUtil.getMiTencentId();
        int status = UserInfoUtil.getAuthStatus();

        if (status == 0) {//未提交

        } else if (status == 10) { //审核失败
            DialogUitl.showSimpleHintDialog(mContext, getString(R.string.prompt), getString(R.string.dialog_title1),
                    true, new DialogUitl.SimpleCallback() {
                        @Override
                        public void onConfirmClick(Dialog dialog, String content) {
                            dialog.dismiss();
                        }
                    });
        } else if (status == 2) {//通过
            submit.setVisibility(View.GONE);
            realName.setEnabled(false);
            cardNumber.setEnabled(false);
            card1Layout.setClickable(false);
            card2Layout.setClickable(false);
            getIdcard();
            NToast.shortToast(this,"实名认证审核通过");
        } else if (status == 1) {//审核中
            getIdcard();
            DialogUitl.showSimpleHintDialog(mContext, getString(R.string.prompt), getString(R.string.dialog_title),
                    true, new DialogUitl.SimpleCallback() {
                        @Override
                        public void onConfirmClick(Dialog dialog, String content) {
                            dialog.dismiss();
                            finish();
                        }
                    });
        }

        uploadOssUtils = new CosXmlUtils(this);
        uploadOssUtils.setOssResultListener(this);

        // 自定义图片加载器
        ISNav.getInstance().init(new ImageLoader() {
            @Override
            public void displayImage(Context context, String path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });
        cardNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.toString().length()==18){
                    isCard=true;
                }else{
                    isCard=false;
                }
            }
        });
    }

    private void tickPhoto() {
        // 自由配置选项
        ISListConfig config = new ISListConfig.Builder()
                // 是否多选, 默认true
                .multiSelect(false)
                // 是否记住上次选中记录, 仅当multiSelect为true的时候配置，默认为true
                .rememberSelected(false)
                // “确定”按钮背景色
                .btnBgColor(Color.TRANSPARENT)
                // “确定”按钮文字颜色
                .btnTextColor(Color.WHITE)
                // 使用沉浸式状态栏
                .statusBarColor(Color.parseColor("#ffd100"))
                // 返回图标ResId
                .backResId(R.drawable.head_back)
                // 标题
                .title(getString(R.string.per_1))
                // 标题文字颜色
                .titleColor(Color.WHITE)
                // TitleBar背景色
                .titleBgColor(Color.parseColor("#ffd100"))
                // 裁剪大小。needCrop为true的时候配置
//                .cropSize(1, 1, 400, 400)
                .needCrop(true)
                // 第一个是否显示相机，默认true
                .needCamera(true)
                // 最大选择图片数量，默认9
                .maxNum(9)
                .build();

// 跳转到图片选择器
        ISNav.getInstance().toListActivity(this, config, REQUEST_CODE_SELECT);

    }

    ArrayList<String> paths = new ArrayList<>();
    UploadDialog dialog;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.submit:
                if (TextUtils.isEmpty(realName.getText().toString().trim())){
                    NToast.shortToast(this,"请输入姓名");
                    return;
                }
                if (TextUtils.isEmpty(cardNumber.getText().toString().trim())){
                    NToast.shortToast(this,"请输入身份证号");
                    return;
                }
                if(!isCard){
                    NToast.shortToast(this,"请输入正确的身份证号");
                    return;
                }
                paths.clear();
                if (path1.equals("") || path2.equals("")) {
                    Toast.makeText(this, R.string.daren_message, Toast.LENGTH_SHORT).show();
                    return;
                }
                paths.add(path1);
                paths.add(path2);

                dialog = new UploadDialog(this);
                dialog.show(300);
                dialog.uploadPhoto(paths.size());
                ossUploadList();
                break;
            case R.id.card1Layout:
                flag = 1;
                tickPhoto();
                break;
            case R.id.card2Layout:
                flag = 2;
                tickPhoto();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
//            case PhotoUtils.INTENT_CROP:
//            case PhotoUtils.INTENT_TAKE:
//            case PhotoUtils.INTENT_SELECT:
//                photoUtils.onActivityResult(this, requestCode, resultCode, data);
//                break;
            case 1:
                if (data != null) {
                    ArrayList<Uri> list = data.getParcelableArrayListExtra("android.intent.extra.RETURN_RESULT");
                    if (flag == 1) {
                        path1 = list.get(0).getPath();
//                        Glide.with(ShiMingRenZhengActivity.this).load(path1).into(mengban1);
                    } else if (flag == 2) {
                        path2 = list.get(0).getPath();
//                        Glide.with(ShiMingRenZhengActivity.this).load(path2).into(mengban2);
                    }
//                    popupWindow.dismiss();
                }
                break;
            case REQUEST_CODE_SELECT:
//                if (data != null) {
//                    ArrayList<Uri> list = data.getParcelableArrayListExtra("android.intent.extra.RETURN_RESULT");
//                    if (flag == 1) {
//                        path1 = list.get(0).getPath();
//                    }
////                    popupWindow.dismiss();
//                    break;
//                }

                //添加图片返回
                if (data != null) {
                    List<String> pathList = data.getStringArrayListExtra("result");
                    if (flag == 1) {
                        path1 = pathList.get(0);
                        Glide.with(ShiMingRenZhengActivity.this).load(path1).into(mengban1);
                    } else if (flag == 2) {
                        path2 = pathList.get(0);
                        Glide.with(ShiMingRenZhengActivity.this).load(path2).into(mengban2);
                    }
                }
//                if (data != null && requestCode == REQUEST_CODE_SELECT) {
//                    images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
//                    if (images != null) {
//                        if (flag == 1) {
//                            path1 = images.get(0).path;
//                            Glide.with(ShiMingRenZhengActivity.this).load(path1).into(mengban1);
//                        } else if (flag == 2) {
//                            path2 = images.get(0).path;
//                            Glide.with(ShiMingRenZhengActivity.this).load(path2).into(mengban2);
//                        }
//                    }
//                }
                break;
        }
    }

    private void verifyIdcard(){
        String body = "";
        try {
            body = new JsonBuilder()
                    .put("idcard_front", path1)
                    .put("idcard_back", path2)
                    .put("idcard_no", cardNumber.getText().toString())
                    .put("real_name", realName.getText().toString())
                    .build();
        } catch (JSONException e) {
        }
        OKHttpUtils.getInstance().getRequest("app/user/verifyIdcard", body, new RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                NToast.shortToast(mContext, errInfo);
                finish();
            }
            @Override
            public void onSuccess(String result) {
                UserInfoUtil.setAuthStatus(1);
                DialogUitl.showSimpleHintDialog(mContext,
                        getString(R.string.prompt),
                        getString(R.string.dialog_title),
                        true, new DialogUitl.SimpleCallback() {
                            @Override
                            public void onConfirmClick(Dialog dialog, String content) {
                                dialog.dismiss();
                                finish();
                            }
                        });
            }
        });
    }

    private void getIdcard(){
        OKHttpUtils.getInstance().getRequest("app/user/getIdcard", "", new RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
            }
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject data = new JSONObject(result);
                    String idcard_front = data.getString("idcard_front");
                    String idcard_back = data.getString("idcard_back");
                    String idcard_no = data.getString("idcard_no");
                    String real_name = data.getString("real_name");
                    realName.setText(real_name);
                    cardNumber.setText(idcard_no);
                    Glide.with(ShiMingRenZhengActivity.this).load(CommonUtils.getUrl(idcard_front)).into(mengban1);
                    Glide.with(ShiMingRenZhengActivity.this).load(CommonUtils.getUrl(idcard_back)).into(mengban2);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void ossUploadList() {
        //（1:个人相册 2:个人视频 3:个人语音介绍 4:达人认证 5:实名认证 6:个人头像 11:动态图片 12:动态视频 21:直播间封面）
        uploadOssUtils.ossUploadList(paths, "image", 5, userId, dialog);
    }

    @Override
    public void ossResult(ArrayList<OssImageResponse> response) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("response", response);
        Message message = new Message();
        message.what = 1;
        message.setData(bundle);
        handler.sendMessage(message);
    }

    @Override
    public void ossVideoResult(ArrayList<OssVideoResponse> response) {

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    ArrayList<OssImageResponse> response = (ArrayList<OssImageResponse>) msg.getData().getSerializable("response");
                    for (int i = 0; i < response.size(); i++) {
                        OssImageResponse response1 = response.get(i);
                        switch (i) {
                            case 0:
                                path1 = response1.getObject();
                                localPath1 = response1.getFull_url();
                                Glide.with(ShiMingRenZhengActivity.this).load(localPath1).into(mengban1);
                                break;
                            case 1:
                                path2 = response1.getObject();
                                localPath2 = response1.getFull_url();
                                Glide.with(ShiMingRenZhengActivity.this).load(localPath2).into(mengban2);
                                break;
                        }
                    }
                    dialog.dissmis();
                    verifyIdcard();
                    break;
            }
        }
    };

}
